<?php

class Business extends CActiveRecord
{
	/**
	 * The followings are the available columns in table 'Business':
	 * @var integer $id
	 * @var string $username
	 * @var string $password
	 * @var string $name
	 * @var string $introduction
	 * @var string $address
	 * @var string $linkman
	 * @var string $linkman_tel
	 * @var string $fax
	 * @var string $email
	 * @var string $qq
	 * @var string $url
	 * @var string $area
	 * @var string $product_sort
	 * @var string $logo
	 * @var string $pic1
	 * @var string $pic2
	 * @var string $pic3
	 * @var integer $recommend
	 * @var integer $state
	 * @var string $create_time
	 * @var string $update_time
	 */
	
	public $province;
	public $city;
	public $repassword;
	public $logo1;
	
	public $province_name;
	public $city_name;
	public $area_name;
	
	public $product_sum;
	
	/**
	 * Returns the static model of the specified AR class.
	 * @return CActiveRecord the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'business';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		return array(
			array('username','length','max'=>16,'min'=>2),
			array('username', 'application.extensions.jformvalidate.ECustomJsValidator','on'=>'create',
			'rules'=> array('remote' => '?r=business/isUniqueUsername'),
			'messages'=> array('remote' => '{attribute}已被注册！'),
			),
			//array('username', 'match' ,'pattern' => '/^[a-zA-z][a-zA-z0-9_]*$/','message'=>'{attribute} 必须为英文字母或"_"'),
			array('password,repassword','length','max'=>16),
			array('repassword','compare', 'compareAttribute' => 'password'),
			array('name','length','min'=>4,'max'=>50),
			array('address','length','min'=>9,'max'=>50),
			array('linkman','length','min'=>2,'max'=>7),
			array('linkman_tel,tel','length','max'=>18),
			array('fax','length','max'=>18),
			array('fax,linkman_tel,tel', 'match' ,'pattern' => '/^[0-9 \+ -]+$/'), 
			array('email','length','max'=>100),
			array('email','email'),
			array('qq','length','min'=>5,'max'=>10),
			array('qq', 'numerical', 'integerOnly'=>true),
			array('url','length','min'=>6,'max'=>100),
			array('url','url'),
			array('area','length','max'=>10),
			array('product_sort','length','max'=>200),
			array('logo','length','max'=>100),
			array('pic1','length','max'=>100),
			array('pic2','length','max'=>100),
			array('pic3','length','max'=>100),
			array('introduction','length','min'=>10),
			array('brand','length','max'=>5),
			array('introduction,username,password,repassword,name,address,linkman,linkman_tel,tel,area,product_sort,logo,logo1,brand', 'required'),
		);
	}

	/**
	 * Prepares attributes before performing validation.
	 */
	protected function beforeValidate($on)//在验证之前将无需用户输入的字段写入
	{
		if($this->isNewRecord){
			$this->state=Yii::app()->params['business_verify'];
			$this->recommend1=0;
			$this->recommend2=0;
			$this->recommend3=0;
			$this->create_time=date('Y-m-d G:i:s');
		}
		$this->update_time=date('Y-m-d G:i:s');
		$this->logo=$this->logo1;
		if(isset($_POST['Business']['product_sort'])){//存在权限 数组转换为字符串
			$sort1=array();
			foreach($this->product_sort as $val){
				if(strlen($val)>2) $sort1[substr($val,0,2)]=1;
			}
			$this->product_sort=implode(',',$this->product_sort);
			foreach($sort1 as $key=>$val){
				$this->product_sort=$key.','.$this->product_sort;
			}
		}
		else
			$this->product_sort='';	
		return true;
	}
	
	protected function afterValidate($on){
		if($this->hasErrors()) return;
		$this->pic1='';$this->pic2='';$this->pic3='';
		$basePath=Yii::app()->basePath.'/../';
 		preg_match_all('/\ssrc=\"([^\"]*)\"/iUs',$this->introduction,$match);
 		foreach($match[1] as $key=>$val){
 			if($key>2) break;
 			$path=preg_replace('/temp\//','',$val);
 			$pic_name='pic'.($key+1);
 			$this->$pic_name=$path;
 			@mkdir(dirname($basePath.$path), 0777,true);
			@fclose(fopen(dirname($basePath.$path).'/index.htm', 'w'));
			@rename($basePath.$val,$basePath.$path);
 		}
 		$this->introduction=preg_replace('/upload\/temp\/business\//','upload/business/',$this->introduction);
 		
 		if($this->logo=='') return;
 		$logo_path=$this->logo;
 		$this->logo=preg_replace('/temp\//','',$this->logo);
 		@mkdir(dirname($basePath.$this->logo), 0777,true);
		@fclose(fopen(dirname($basePath.$this->logo).'/index.htm', 'w'));
		$image = Yii::app()->image->load($basePath.$logo_path);
		$image->resize(170,90);
		$image->save();
		@rename($basePath.$logo_path,$basePath.$this->logo);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => '索引号',
			'username' => '用户名',
			'password' => '密码',
			'repassword' => '重复密码',
			'name' => '企业名称',
			'brand' => '品牌',
			'introduction' => '厂商简介',
			'address' => '地址',
			'tel' => '厂商电话',
			'linkman' => '负责人',
			'linkman_tel' => '负责人电话',
			'fax' => '传真',
			'email' => 'E-mail',
			'qq' => 'QQ',
			'url' => '网站',
			'area' => '所在地',
			'product_sort' => '产品所属分类',
			'logo' => '厂商标识',
			'pic1' => '企业图片1',
			'pic2' => '企业图片2',
			'pic3' => '企业图片3',
			'recommend1' => '文字',//0 正常 1 推荐
			'recommend2' => '全部',//0 正常 1 推荐
			'recommend3' => '分类',//0 正常 1 推荐
			'state' => '状态',//0正常 1删除 2未审核
			'create_time' => '添加时间',
			'update_time' => '最后更新时间',
			'product_sum' => '产品数量',
			'province_name' => '所在地'
		);
	}
	
	public function safeAttributes(){
		$params=parent::safeAttributes();
		$params[]='province';
		$params[]='city';
		$params[]='repassword';
		$params[]='logo1';
		return $params;
	}
}